using Backpack;
using SO;
using TMPro;
using UnityEngine;
public interface IItemCell
{
    public void UpdateCell(ItemCellData itemCellData);
}
public class ItemCell : MonoBehaviour, IItemCell
{
    public GameObject itemUIOriginPrefab;
    public TextMeshProUGUI amountTmp;
    private IItemUI itemUI;
    private void Awake()
    {
        amountTmp = GetComponentInChildren<TextMeshProUGUI>();

        // itemUIOriginPrefab = 
    }
    public void UpdateCell(ItemCellData itemCellData)
    {
        if (itemCellData.itemInventoryData.amount != 0)
        {
            if (itemUI == null)
            {
                GameObject itemUIGObj = Instantiate(itemUIOriginPrefab, transform);

                itemUIGObj.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;

                itemUI = itemUIGObj.GetComponent<IItemUI>();

                itemUI.Init(ItemConfigModel.Instance.GetData(itemCellData.itemInventoryData.id));

                itemUI.GameObject.transform.SetAsFirstSibling();
            }
        }
        else
        {
            if (itemUI != null)
            {
                Destroy(itemUI.GameObject);
                itemUI = null;
            }
        }
        UpdateaAmountTmp(itemCellData.itemInventoryData.amount);
    }
    private void UpdateaAmountTmp(int amount)
    {
        if (amount == 0)
        {
            amountTmp.gameObject.SetActive(false);
            return;
        }
        amountTmp.gameObject.SetActive(true);
        amountTmp.text = amount.ToString(); //更新数量
    }
}